package com.android.lovingpet.adopt.host.behost.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.adopt.host.behost.BeHostPresenter;
import com.android.lovingpet.adopt.host.behost.IBeHostContract;
import com.android.lovingpet.adopt.host.behost.PetExp;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.mine.pet.view.EditPetFragment;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ikata on 2018/4/27.
 */

public class ExperienceFragment extends BaseFragment<IBeHostContract.View, BeHostPresenter> implements IBeHostContract.View, View.OnClickListener {
    @BindView(R.id.rl_pet_data)
    RelativeLayout rlPetData;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.rv_pet_experience_content)
    RecyclerView rv_pet;
    @BindView(R.id.tv_save)
    TextView tvSave;
    private PetRvAdapter petRvAdapter;
    Unbinder unbinder;

    @Override
    protected int setContentView() {
        return R.layout.fragment_experience;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder = ButterKnife.bind(this, view);
        initView();
        setTitle(view,"养宠经历");
    }

    @Override
    public BeHostPresenter createPresenter() {
        return new BeHostPresenter();
    }

    @Override
    public IBeHostContract.View createView() {
        return this;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void initView() {
        rv_pet.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        petRvAdapter = new PetRvAdapter(new ArrayList<>());
        rv_pet.setAdapter(petRvAdapter);
        rv_pet.setItemAnimator(new DefaultItemAnimator());
    }

    @OnClick({R.id.iv_add, R.id.rl_pet_data,R.id.tv_save})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_add:
                petRvAdapter.add();
                break;
            case R.id.rl_pet_data:
                getFragmentManager().beginTransaction().replace(R.id.beHost_content,new EditPetFragment()).commit();
                break;
            case R.id.tv_save:
                List<PetExp> list=petRvAdapter.getList();
                if (list==null||list.size()==0)
                {
                    Toast.makeText(getContext(),"请添加养宠经验!",Toast.LENGTH_LONG).show();
                }
                else {
                    EventBus.getDefault().postSticky(list);
                    getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.beHost_content,new FamilyInfoFragment()).commit();
                }
                break;
        }
    }


    @Override
    public void next(@Nullable Bundle bundle) {

    }
}
